<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.name" clearable placeholder="输入商品名称" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.ruleType" clearable placeholder="规则类型" class="filter-item" style="width: 150px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-select v-model="query.pid" clearable placeholder="商品分类" class="filter-item" filterable style="width: 200px">
          <el-option v-for="item in optionsMetaShow" :disabled="item.disabled === 0"
            :value="item.value"
            :key="item.id"
            :label="item.label">
          </el-option>
        </el-select>
        <rrOperation :crud="crud" />
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-loading"
          :loading="loading"
          @click="toPriceLoading"
        >
        一键刷新价格
        </el-button>
      </div>
      
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <configRuleForm ref="formRule" :ruleName="ruleName" :permission="permission" :ruleDetailList ="ruleDetailList"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="550px">
        <el-form ref="form" :model="form" :rules="rules" size="small">
          <el-form-item label="最大折扣价" prop="maxPrice">
            <el-input v-model="form.maxPrice" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('cateName')" prop="cateName" label="分类上级名称" />
        <el-table-column v-if="columns.visible('name')" prop="name" label="名称" show-overflow-tooltip/>
        <el-table-column v-if="columns.visible('maxPrice')" prop="maxPrice" label="最大折扣价" />
        <el-table-column label="规则类型" align="center">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.ruleType == 0">手机</el-tag>
            <el-tag v-else>平板</el-tag>
        </template>
      </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建日期" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="修改日期" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','syCategoryRule:edit','syCategoryRule:del','syCategoryRule:config']" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-button
              size="mini" v-permission="['admin','syCategoryRule:config']"
              type="success"
              @click="configRule(scope.row)"
              >规则配置</el-button>
              &nbsp;&nbsp;
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyCategoryRule from '@/api/syCategoryRule'
import {getCateList,getCateDetail,getPriceLoading} from '@/api/syCategoryRule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import configRuleForm from './formRule'
import { Message } from 'element-ui'
// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/syCategoryRule', sort: 'id,pid,desc', crudMethod: { ...crudSyCategoryRule }})
const defaultForm = { id: null, cateId: null, pid: null, name: null, maxPrice: null, ruleType: null, createTime: null, updateTime: null }
export default {
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList, configRuleForm },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      loading: false,
      optionsMetaShow: [],
      eId: null,
      ruleName : '规则配置',
      ruleDetailList:[],
      permission: {
        configRuleForm: ['admin', 'syCategoryRule:config'],
        add: ['admin', 'syCategoryRule:add'],
        edit: ['admin', 'syCategoryRule:edit'],
        del: ['admin', 'syCategoryRule:del']
      },
      rules: {
        cateId: [
          { required: true, message: '分类id不能为空', trigger: 'blur' }
        ],
        pid: [
          { required: true, message: '分类上级id不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        maxPrice: [
          { required: true, message: '最大折扣价不能为空', trigger: 'blur' }
        ],
        ruleType: [
          { required: true, message: '规则类型：0手机 1平板不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: '0', display_name: '手机' },
        { key: '1', display_name: '平板' }
      ]
    }
  },
  watch: {
  },
  created() {
    getCateList().then(res => {
        this.optionsMetaShow = res.cateList
    })
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.cateId
        delete this.crud.params.pid
        delete this.crud.params.name
        delete this.crud.params.ruleType
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    configRule(data) {
      this.ruleName = " "+data.name+".......最大折扣价："+data.maxPrice+" ";
      getCateDetail(data.cateId).then(res => {
        this.ruleDetailList =res.data;
        const _this = this.$refs.formRule
        _this.dialog = true
      })
    },
    toPriceLoading(){
      const query = this.query;
      console.info("----------------"+JSON.stringify(query));
      if (query.pid && query) {
        this.loading = true
        getPriceLoading(query.pid).then(res => {
          this.loading = false
          this.crud.toQuery();
          Message({ message: '刷新成功', type: 'success' })
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
          this.$notify.error({
            title: '错误',
            message: err.response.data.message,
            duration: 0
          });
        })
      }else{
        this.$notify.error({
          title: '错误',
          message: '必须选择一个商品分类'
        });
        return;
      }
    },
    refreshParentPage(){
      this.crud.toQuery();
    }
  }
  
}

</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
